package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 功能：
 * 作者：4204001140
 * 日期：2024/4/15 13:51
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     *
     * 新增套餐的话，套餐内包含着套餐和菜品的关系，所以需要新增套餐和菜品的关系
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        // 获取返回的套餐ID
        Long setmealId = setmeal.getId();
        // 将套餐和菜品的关系存入数据库
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
            setmealDishMapper.insert(setmealDish);
        }
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        /**
         * 逻辑：
         * 1.使用pageHelper设置分页参数，实现分页
         * 2.mapper查询返回Page<T> pages
         * 3.获取套餐下的套餐和菜品的关系
         * 4.返回PageResult(pages.getTotal(),pages.getRecords())
         */
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> pages = setmealMapper.pageQuery(setmealPageQueryDTO);
        for (SetmealVO setmealVO : pages.getResult()) {
            List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(setmealVO.getId());
            setmealVO.setSetmealDishes(setmealDishes);
        }
        return new PageResult(pages.getTotal(),pages.getResult());
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        /**
         * 逻辑：
         * 套餐内有起售的菜品不能删除
         * 删除套餐：传入的是套餐的ID
         * 同时需要删除套餐内套餐和菜品的关系
         */
        // 查看是否全部禁售
        ids.forEach(id -> {
            SetmealVO setmeal = setmealMapper.getById(id);
            if (StatusConstant.ENABLE.equals(setmeal.getStatus())){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        ids.forEach(id ->{
            // 删除关系数据
            setmealDishMapper.deleteBySetmealId(id);
            setmealMapper.deleteById(id);
        });


    }

    /**
     * 根据id查询套餐详情
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        /**
         * 逻辑：
         * 1.根据套餐id查询套餐信息
         *      左连接查询分类名称
         * 2.根据套餐id查询套餐内的菜品
         */
        SetmealVO setmealVO = setmealMapper.getById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 更新套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        /**
         * 逻辑：
         * 删除原来的套餐和菜品的关系
         * 重写关系
         * 更新套餐的信息
         */


        // 删除原来的关系
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        // 重新建立关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish ->{
            setmealDish.setSetmealId(setmealDTO.getId());
            setmealDishMapper.insert(setmealDish);
        });

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

    }

    /**
     * 启售、停售套餐
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //起售套餐时，如果套餐内包含停售的菜品，则不能起售
        if (StatusConstant.ENABLE.equals(status)){
            List<Dish> dishs = dishMapper.getBySetmealId(id);
            dishs.forEach(dish -> {
                if (StatusConstant.DISABLE.equals(dish.getStatus())){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
